<?php
/**
 * DynaGrid - The PHP datagrid
 *
 * @package    DynaGrid
 * @author     $Author: keesschepers $
 * @version    $Id: Grid.php 20 2011-02-16 17:44:35Z keesschepers $
 */

include_once('DataSource.php');
include_once('Exception.php');
include_once('HTML/Table.php');
include_once('HTML/Interface.php');
include_once('Url.php');
//include_once('PageBrowser.php');

final class DynaGrid_Grid implements DynaGrid_Html_Interface {
	/**
	 * The table object draws the table.
	 *
	 * @var DynaGrid_Table
	 */
	private $table;
	/**
	 * The dataset will communicate between the data and presentation layer.
	 *
	 * @var DynaGrid_DataSource
	 */
	private $datasource;
	/**
	 * The pagebrowser is responsible for drawing the page browser
	 * below in the datagrid.
	 *
	 * @var DynaGrid_PageBrowser
	 */
	private $pagebrowser;
	/**
	 * Message which will be displayed when there are no rows found.
	 *
	 * @var string
	 */
	public $norowsmessage = 'There are no records found.';
	/**	 
	 * Initialisation
         *
         * Pass thru any argument which is given in the constructor to the
         * datasource which can be used by the datasource Adapter.
	 * 
	 */
	public function __construct() {
		
		if(!DynaGrid_DataSource::$dataSourceAdapter instanceof DynaGrid_DataSource_Adapter_Interface) {
			include_once 'Exception.php';
      throw new DynaGrid_Exception('The datasource Adapter is invalid or not set.');
		}

		DynaGrid_DataSource::$dataSourceAdapter->setArguments(func_get_args());

		$this->datasource = new DynaGrid_DataSource();

		/**
		 * @todo make orderway and orderfield dynamic, using $_GET is foolish
		 */
		if(isset($_GET['orderway']) && isset($_GET['orderfield'])) {
			$this->datasource->setOrderWay($_GET['orderway']);
			$this->datasource->setOrderField($_GET['orderfield']);
		}

		$this->table = new DynaGrid_HTML_Table($this->datasource);
		$this->table->class = 'dynagrid';
	}
	/**
	 * Split the results over several pages.
	 *
	 * @param integer $limitPerPage number of records per page, 0 = unlimited
	 * @param integer $browseoffset number of pages displayed in the browser
	 */
	public function splitResults($limitPerPage,$browseoffset=8, $pagebrowser = null) {
		if($limitPerPage > 0) {

			if($pagebrowser instanceof DynaGrid_PageBrowser_Interface) {
				$this->pagebrowser = $pagebrowser;
			} else {
				include_once('PageBrowser/Default.php');
				$this->pagebrowser = new DynaGrid_PageBrowser_Default(new DynaGrid_Url($_SERVER['QUERY_STRING']));
			}

			$this->pagebrowser->setLimit($limitPerPage);
			$this->pagebrowser->setBrowseOffset($browseoffset);

			$this->datasource->maxRows = $limitPerPage;
			$this->datasource->offset = $this->pagebrowser->getCurrentPage()*$limitPerPage;
		}
	}
	/**
	 * Replace the name of a column. Default DynaGrid will use the name from the select query.
	 *
	 * @param string $column
	 * @param string $title
	 */
	public function replaceHeader($column,$title) {
		if(false == ($columnobj = $this->table->columns->getColumn($column,'header'))) {
			$this->table->columns->add($column,$title);
			$columnobj = $this->table->columns->getColumn($column,'header');
		}
		$columnobj->setTitle($title);
	}
	/**
	 * Set the position of a column
	 *
	 * @param string $column the column name you want to re-order
	 * @param string $position the column name where the column comes AFTER root is before first column
	 */
	public function setColumnPosition($column,$position='root') {
		$this->table->columns->setColumnPosition($column,$position);
	}
	/**
	 * Add a dynamic column. For instance if you want to add a edit button
	 * where in the like the id of the record is set (%id% or {$id}).
	 *
	 * @param string $name name of the column
	 * @param string $data the value/data off the column placeholders allowed
	 * @param string $orderfield the table field where the column is sorted on with the arrows
	 */
	public function addColumn($name,$data,$position='root',$orderfield=null) {
		$this->table->columns->addDynamic($name,$position);			
		
		$column = $this->table->columns->getDynamicColumn($name,'body');
		$column->setCallbackData($data);
		
		if(!is_null($orderfield)) {
			$column = $this->table->columns->getDynamicColumn($name,'header');
			$column->setOrderField($orderfield);
		}
	}
	/**
	 * Set the caption (title) of the table.
	 *
	 * @param string $title
	 */
	public function setCaption($title) {
		$this->table->caption->setTitle($title);
	}
	/**
	 * Builds the table.
	 *
	 * @return string HTML output
	 */
	public function getHTML() {	
		$this->datasource->norowsmessage = $this->norowsmessage;
		$this->datasource->process();
		$output = $this->table->getHTML();
		
		if($this->pagebrowser instanceof DynaGrid_PageBrowser_Interface && $this->datasource->totalRows > 0) {
			$this->pagebrowser->setTotalRecords($this->datasource->totalRows);
			$output .= $this->pagebrowser->getHTML();
		}		
		return $output;
	}
	/**
	 * Register the datasource Adapter which talks with the data for gaining meta-data information about it.
	 * 
	 * @param DynaGrid_DataSource_Adapter_Interface $dataSourceAdapter
	 */
	public static function registerDataSourceAdapter(DynaGrid_DataSource_Adapter_Interface $dataSourceAdapter) {
			DynaGrid_DataSource::$dataSourceAdapter = $dataSourceAdapter;
	}
}